home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / cookie1.el < prev    next >
Lisp/Scheme  |  1993-09-16  |  6KB  |  165 lines

  1. ;;; cookie1.el --- retrieve random phrases from fortune cookie files
  2.  
  3. ;; Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Maintainer: FSF
  7. ;; Keywords: games
  8. ;; Created: Mon Mar 22 17:06:26 1993
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; Support for random cookie fetches from phrase files, used for such
  29. ;; critical applications as emulating Zippy the Pinhead and confounding
  30. ;; the NSA Trunk Trawler.
  31. ;;
  32. ;; The two entry points are `cookie' and `cookie-insert'.  The helper
  33. ;; function `shuffle-vector' may be of interest to programmers.
  34. ;;
  35. ;; The code expects phrase files to be in one of two formats:
  36. ;;
  37. ;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
  38. ;; leading whitespace ignored).
  39. ;;
  40. ;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
  41. ;;
  42. ;; Everything up to the first delimiter is treated as a comment.  Other
  43. ;; formats could be supported by adding alternates to the regexp
  44. ;; `cookie-delimiter'.
  45. ;;
  46. ;; This code derives from Steve Strassman's 1987 spook.el package, but
  47. ;; has been generalized so that it supports multiple simultaneous
  48. ;; cookie databases and fortune files.  It is intended to be called
  49. ;; from other packages such as yow.el and spook.el.
  50. ;;
  51. ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
  52. ;; format and do the right thing.
  53.  
  54. ;;; Code:
  55.  
  56. ; Randomize the seed in the random number generator.
  57. (random t)
  58.  
  59. (defconst cookie-delimiter "\n%%\n\\|\0"
  60.   "Delimiter used to separate cookie file entries.")
  61.  
  62. (defvar cookie-cache (make-vector 511 0)
  63.   "Cache of cookie files that have already been snarfed.")
  64.  
  65. ;;;###autoload
  66. (defun cookie (phrase-file startmsg endmsg)
  67.   "Return a random phrase from PHRASE-FILE.  When the phrase file
  68. is read in, display STARTMSG at beginning of load, ENDMSG at end."
  69.   (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
  70.     (shuffle-vector cookie-vector)
  71.     (aref cookie-vector 1)))
  72.  
  73. ;;;###autoload
  74. (defun cookie-insert (phrase-file &optional count startmsg endmsg)
  75.   "Insert random phrases from PHRASE-FILE; COUNT of them.  When the phrase file
  76. is read in, display STARTMSG at beginning of load, ENDMSG at end."
  77.   (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
  78.     (shuffle-vector cookie-vector)
  79.     (let ((start (point)))
  80.       (insert ?\n)
  81.       (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
  82.       (insert ?\n)
  83.       (fill-region-as-paragraph start (point) nil))))
  84.  
  85. (defun cookie1 (arg cookie-vec)
  86.   "Inserts a cookie phrase ARG times."
  87.   (cond ((zerop arg) t)
  88.     (t (insert (aref cookie-vec arg))
  89.        (insert " ")
  90.        (cookie1 (1- arg) cookie-vec))))
  91.  
  92. ;;;###autoload
  93. (defun cookie-snarf (phrase-file startmsg endmsg)
  94.   "Reads in the PHRASE-FILE, returns it as a vector of strings.
  95. Emit STARTMSG and ENDMSG before and after.  Caches the result; second
  96. and subsequent calls on the same file won't go to disk."
  97.   (let ((sym (intern-soft phrase-file cookie-cache)))
  98.     (and sym (not (equal (symbol-function sym)
  99.              (nth 5 (file-attributes phrase-file))))
  100.      (yes-or-no-p (concat phrase-file
  101.                   " has changed.  Read new contents? "))
  102.      (setq sym nil))
  103.     (if sym
  104.     (symbol-value sym)
  105.       (setq sym (intern phrase-file cookie-cache))
  106.       (message startmsg)
  107.       (save-excursion
  108.     (let ((buf (generate-new-buffer "*cookie*"))
  109.           (result nil))
  110.       (set-buffer buf)
  111.       (fset sym (nth 5 (file-attributes phrase-file)))
  112.       (insert-file-contents (expand-file-name phrase-file))
  113.       (re-search-forward cookie-delimiter)
  114.       (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
  115.         (let ((beg (point)))
  116.           (re-search-forward cookie-delimiter)
  117.           (setq result (cons (buffer-substring beg (1- (point)))
  118.                  result))))
  119.       (kill-buffer buf)
  120.       (message endmsg)
  121.       (set sym (apply 'vector result)))))))
  122.  
  123. (defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match)
  124.   "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
  125. STARTMSG and ENDMSG are passed along to `cookie-snarf'.
  126. Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
  127.   ;; Make sure the cookies are in the cache.
  128.   (or (intern-soft phrase-file cookie-cache)
  129.       (cookie-snarf phrase-file startmsg endmsg))
  130.   (completing-read prompt
  131.            (let ((sym (intern phrase-file cookie-cache)))
  132.              ;; We cache the alist form of the cookie in a property.
  133.              (or (get sym 'completion-alist)
  134.              (let* ((alist nil)
  135.                 (vec (cookie-snarf phrase-file
  136.                            startmsg endmsg))
  137.                 (i (length vec)))
  138.                (while (> (setq i (1- i)) 0)
  139.                  (setq alist (cons (list (aref vec i)) alist)))
  140.                (put sym 'completion-alist alist))))
  141.            nil require-match nil nil))
  142.  
  143. ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
  144. ; [of the University of Birmingham Computer Science Department]
  145. ; for the iterative version of this shuffle.
  146. ;
  147. ;;;###autoload
  148. (defun shuffle-vector (vector)
  149.   "Randomly permute the elements of VECTOR (all permutations equally likely)"
  150.   (let ((i 0)
  151.     j
  152.     temp
  153.     (len (length vector)))
  154.     (while (< i len)
  155.       (setq j (+ i (random (- len i))))
  156.       (setq temp (aref vector i))
  157.       (aset vector i (aref vector j))
  158.       (aset vector j temp)
  159.       (setq i (1+ i))))
  160.   vector)
  161.  
  162. (provide 'cookie1)
  163.  
  164. ;;; cookie1.el ends here
  165.